/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制音效管理器的窗口
* CreateTime：2020-07-07 16:31:20
* Version：1.0
* Modify Recorder：
*************************************************/

using System.Collections.Generic;
using TinyToolKit;
using TinyToolKit.Sound;
using UnityEditor;
using UnityEngine;

namespace TinyToolKitEditor
{
    public class DrawWindowSound : DrawWindowBase
    {
        private bool isFoldoutMusic = true;
        private bool isFoldoutFXSound = true;
        private bool isFoldoutUISound = true;
        private bool isFoldoutAudiosPool = true;
        private TSoundManager _soundManager;
        private Vector2 soundScrollPos;

        private SerializedObject _serializedObject;
        private SerializedProperty globalVolume;
        private SerializedProperty globalMusicVolume;
        private SerializedProperty globalSoundsVolume;
        private SerializedProperty globalUISoundsVolume;

        private TinyToolSettingAsset _toolSetting;

        public override void InitWindow(ScriptableObject setting)
        {
            _toolSetting = (TinyToolSettingAsset) setting;
            _serializedObject = new SerializedObject(_toolSetting);

            globalVolume = _serializedObject.FindProperty("globalVolume");
            globalMusicVolume = _serializedObject.FindProperty("globalMusicVolume");
            globalSoundsVolume = _serializedObject.FindProperty("globalSoundsVolume");
            globalUISoundsVolume = _serializedObject.FindProperty("globalUISoundsVolume");
        }

        protected override void DrawInternalMainGUI()
        {
            _serializedObject.Update();

            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            _toolSetting.ignoreDuplicateMusic =
                EditorGUILayout.Toggle("同时只播放同一个BG音乐：", _toolSetting.ignoreDuplicateMusic);
            _toolSetting.ignoreDuplicateSounds =
                EditorGUILayout.Toggle("同时只播放同一个FX音效：", _toolSetting.ignoreDuplicateSounds);
            _toolSetting.ignoreDuplicateUiSounds =
                EditorGUILayout.Toggle("同时只播放同一个UI音效：", _toolSetting.ignoreDuplicateUiSounds);
            EditorGUI.EndDisabledGroup();

            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(globalVolume, new GUIContent("全局音量："));
            ProcessBar(_toolSetting.globalVolume, "Global Volume");
            EditorGUILayout.PropertyField(globalMusicVolume, new GUIContent("全局背景音量："));
            ProcessBar(_toolSetting.globalMusicVolume * _toolSetting.globalVolume, "Global BgMusic Volume");
            EditorGUILayout.PropertyField(globalSoundsVolume, new GUIContent("全局FX音量："));
            ProcessBar(_toolSetting.globalSoundsVolume * _toolSetting.globalVolume, "Global FXSounds Volume");
            EditorGUILayout.PropertyField(globalUISoundsVolume, new GUIContent("全局UI音量："));
            ProcessBar(_toolSetting.globalUISoundsVolume * _toolSetting.globalVolume, "Global UISounds Volume");

            TinyToolWindowUtil.DrawDefaultEditorGUI();

            #region Runtime

            if (EditorApplication.isPlaying && TinyTool.Instance.HasManager<TSoundManager>())
            {
                if (_soundManager == null) _soundManager = TinyTool.Instance.GetManager<TSoundManager>();

                Dictionary<int, AudioItem> musicAudiosDic = _soundManager.GetAllAudio(AudioItemType.Music);
                if (musicAudiosDic != null && musicAudiosDic.Count > 0)
                {
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    isFoldoutMusic = EditorGUILayout.Foldout(isFoldoutMusic, "背景音乐"); 
                    if (isFoldoutMusic)
                    {
                        TinyToolWindowUtil.DrawTableHead("名字", "状态");
                        foreach (var audioItem in musicAudiosDic)
                        {
                            string progress = (audioItem.Value.Progress * 100).ToString("0.0") + "%";
                            string volume = audioItem.Value.AudioSource.volume.ToString("0.00");
                            string msg01 = audioItem.Value.Clip.name;
                            string msg02 =
                                $"播放进度：{progress}，音量：{volume}，循环：{audioItem.Value.Loop}，持久化：{audioItem.Value.Persist}";
                            EditorGUILayout.LabelField(msg01, msg02);
                        }

                        TinyToolWindowUtil.DrawTableTail("总计：", musicAudiosDic.Count.ToString());
                    }
                    EditorGUILayout.EndVertical();
                }

                Dictionary<int, AudioItem> soundAudiosDic = _soundManager.GetAllAudio(AudioItemType.FxSound);
                if (soundAudiosDic != null && soundAudiosDic.Count > 0)
                {
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    isFoldoutFXSound = EditorGUILayout.Foldout(isFoldoutFXSound, "特效音乐");
                    if (isFoldoutFXSound)
                    {
                        TinyToolWindowUtil.DrawTableHead("名字", "状态");
                        foreach (var audioItem in soundAudiosDic)
                        {
                            if (audioItem.Value.AudioSource == null) continue;
                            string progress = (audioItem.Value.Progress * 100).ToString("0.0") + "%";
                            string volume = audioItem.Value.AudioSource.volume.ToString("0.00");
                            string msg01 = audioItem.Value.Clip.name;
                            string msg02 =
                                $"播放进度：{progress}，音量：{volume}，循环：{audioItem.Value.Loop}，持久化：{audioItem.Value.Persist}";
                            EditorGUILayout.LabelField(msg01, msg02);
                        }

                        TinyToolWindowUtil.DrawTableTail("总计：", soundAudiosDic.Count.ToString());
                    }
                    EditorGUILayout.EndVertical();
                }

                Dictionary<int, AudioItem> uiSoundAudiosDic = _soundManager.GetAllAudio(AudioItemType.UISound);
                if (uiSoundAudiosDic != null && uiSoundAudiosDic.Count > 0)
                {
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    isFoldoutUISound = EditorGUILayout.Foldout(isFoldoutUISound, "UI音效");
                    if (isFoldoutUISound)
                    {
                        TinyToolWindowUtil.DrawTableHead("名字", "状态");
                        foreach (var audioItem in uiSoundAudiosDic)
                        {
                            string progress = (audioItem.Value.Progress * 100).ToString("0.0") + "%";
                            string volume = audioItem.Value.AudioSource.volume.ToString("0.00");
                            string msg01 = audioItem.Value.Clip.name;
                            string msg02 =
                                $"播放进度：{progress}，音量：{volume}，循环：{audioItem.Value.Loop}，持久化：{audioItem.Value.Persist}";
                            EditorGUILayout.LabelField(msg01, msg02);
                        }

                        TinyToolWindowUtil.DrawTableTail("总计：", uiSoundAudiosDic.Count.ToString());
                    }
                    EditorGUILayout.EndVertical();
                }

                List<string> audiosName = _soundManager.audiosNameInPool;
                if (audiosName != null && audiosName.Count > 0)
                {
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    isFoldoutAudiosPool = EditorGUILayout.Foldout(isFoldoutAudiosPool, "缓存池");
                    if (isFoldoutAudiosPool)
                    {
                        TinyToolWindowUtil.DrawTableHead("ID", "名字");
                        for (int i = 0; i < audiosName.Count; i++)
                        {
                            EditorGUILayout.LabelField((i + 1).ToString(), audiosName[i]);
                        }

                        TinyToolWindowUtil.DrawTableTail("总计：", audiosName.Count.ToString());
                    }
                    EditorGUILayout.EndVertical();
                }
            }

            #endregion

            _serializedObject.ApplyModifiedProperties();
        }

        private void ProcessBar(float value, string label)
        {
            Rect rect = GUILayoutUtility.GetRect(18, 18);
            EditorGUI.ProgressBar(rect, value, label);
            EditorGUILayout.Space();
        }
    }
}